/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service.processor.active;

import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.business.account.facade.order.freeze.FreezeAccountOrder;
import info.peigen.hotpot.business.account.facade.order.freeze.FreezeAmountOrder;
import info.peigen.hotpot.business.account.facade.order.freeze.UnfreezeAccountOrder;
import info.peigen.hotpot.business.account.facade.order.freeze.UnfreezeAmountOrder;
import info.peigen.hotpot.business.customer.core.domain.Customer;
import info.peigen.hotpot.business.customer.core.service.processor.base.AbstractCustomerServiceProcessor;
import info.peigen.hotpot.business.customer.facade.order.favor.CustomerFavorStationOrder;
import info.peigen.hotpot.business.customer.facade.order.login.CustomerLoginOrder;
import info.peigen.hotpot.business.customer.facade.order.update.CustomerUpdateOrder;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.ServiceOrderBase;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

/**
 * <b>(CustomerActiveProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/8/30
 */
@SuppressWarnings("rawtypes")
@Component
public class CustomerActiveProcessor extends AbstractCustomerServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 20;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        String   customerId = switchId(serviceContext);
        Customer domain     = customerRepository.active(customerId);
        Assert.notNull(domain, () -> new BusinessException(ResultCode.FAILURE, "未能激活客户[customerId=" + customerId + "]"));
        domain.check();
        serviceContext.setDomain(domain);
    }

    private String switchId(ProcessorServiceContext serviceContext) {
        ServiceOrderBase order = serviceContext.getOrder();

        if (order instanceof FreezeAccountOrder) {
            return ((FreezeAccountOrder) order).getAccountId();
        } else if (order instanceof UnfreezeAccountOrder) {
            return ((UnfreezeAccountOrder) order).getAccountId();
        } else if (order instanceof FreezeAmountOrder) {
            return ((FreezeAmountOrder) order).getAccountId();
        } else if (order instanceof UnfreezeAmountOrder) {
            return ((UnfreezeAmountOrder) order).getAccountId();
        } else if (order instanceof CustomerUpdateOrder) {
            return ((CustomerUpdateOrder) order).getCustomerId();
        } else if (order instanceof CustomerFavorStationOrder) {
            return ((CustomerFavorStationOrder) order).getCustomerId();
        } else if (order instanceof CustomerLoginOrder) {
            return ((CustomerLoginOrder) order).getCustomerId();
        } else {
            return "";
        }
    }
}
